/**
 * @author YUN
 */
package a.db;

import android.text.TextUtils;

public class SqliteTable extends SqliteObject {

	public SqliteTable(String name) {
		super(name);
	}

	public SqliteTable addIntegerColumn(String columnName) {
		return addIntegerColumn(columnName, null);
	}

	public SqliteTable addIntegerColumn(String columnName, String columnConstraint) {
		add(columnName + " INTEGER", columnConstraint);
		return this;
	}

	public SqliteTable addTextColumn(String columnName) {
		return addTextColumn(columnName, null);
	}

	public SqliteTable addTextColumn(String columnName, String columnConstraint) {
		add(columnName + " TEXT", columnConstraint);
		return this;
	}

	public SqliteTable addNumericColumn(String columnName) {
		return addNumericColumn(columnName, null);
	}

	public SqliteTable addNumericColumn(String columnName, String columnConstraint) {
		add(columnName + " NUMERIC", columnConstraint);
		return this;
	}

	public SqliteTable addBlobColumn(String columnName) {
		return addBlobColumn(columnName, null);
	}

	public SqliteTable addBlobColumn(String columnName, String columnConstraint) {
		add(columnName + " BLOB", columnConstraint);
		return this;
	}

	@Override
	String toDropSql() {
		return "DROP TABLE IF EXISTS " + getName();
	}

	@Override
	String toCreateSql() {
		StringBuilder sql = new StringBuilder();
		// start
		sql.append("CREATE TABLE ").append(getName()).append("(");
		// contents
		boolean addComma = false;
		for (String part : getColumn()) {
			if (addComma) {
				sql.append(",");
			}
			addComma = true;
			sql.append("\n").append(part);
		}
		// end
		sql.append("\n").append(");");
		return sql.toString();
	}

	private void add(String columnPart, String constraintPart) {
		if (TextUtils.isEmpty(constraintPart)) {
			addColumn(columnPart);
		} else {
			addColumn(columnPart + " " + constraintPart);
		}
	}
}
